package pl.iachoo.ctl.tabory.ui.editors.compositions;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.ViewPart;

import pl.iachoo.ctl.tabory.dao.hibernate.interfaces.ICompositionService;
import pl.iachoo.ctl.tabory.dao.hibernate.interfaces.IVagonService;
import pl.iachoo.ctl.tabory.dao.hibernate.services.ServiceManager;
import pl.iachoo.ctl.tabory.dao.hibernate.vo.CompositionVO;
import pl.iachoo.ctl.tabory.dao.hibernate.vo.VagonVO;
import pl.iachoo.ctl.tabory.ui.editors.compositions.composites.CompositionDetailsSectionPart;
import pl.iachoo.ctl.tabory.ui.editors.compositions.composites.CompositionTableSectionPart;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;

public class CompositionEditor extends ViewPart {

	public static final String ID = "pl.iachoo.ctl.tabory.ui.editors.compositions.CompositionEditor";

	private final static Logger log = Logger.getLogger(CompositionEditor.class);

	private CompositionSelection compositionSelection;
	private final FormToolkit formToolkit = new FormToolkit(
			Display.getDefault());

	private CompositionTableSectionPart compositeTableSectionPart;

	private CompositionDetailsSectionPart compositeDetailsSectionPart;

	public CompositionEditor() {
		compositionSelection = new CompositionSelection();
		try {
			IVagonService vagonService = ServiceManager.getVagonService();
			ICompositionService compositionService = ServiceManager
					.getCompositionService();
			List<CompositionVO> allCompositions = compositionService
					.getAllCompositions();
			List<VagonVO> allVagons = vagonService.getAllVagons();
			compositionSelection.setListAllVagons(allVagons);
			compositionSelection.setListComposition(allCompositions);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	@Override
	public void createPartControl(Composite parent) {

		Form frmNewForm = formToolkit.createForm(parent);
		formToolkit.paintBordersFor(frmNewForm);
		frmNewForm.getBody().setLayout(new GridLayout(2, false));

		compositeTableSectionPart = new CompositionTableSectionPart(
				frmNewForm.getBody(), formToolkit, "sdf");
		Section section = compositeTableSectionPart.getSection();
		section.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1,
				1));
		formToolkit.paintBordersFor(section);

		compositeDetailsSectionPart = new CompositionDetailsSectionPart(
				frmNewForm.getBody(), formToolkit, 0);
		Section sctncompositionsdetailsname = compositeDetailsSectionPart
				.getSection();
		sctncompositionsdetailsname.setLayoutData(new GridData(SWT.FILL,
				SWT.FILL, true, false, 1, 1));
		formToolkit.paintBordersFor(sctncompositionsdetailsname);

		compositeTableSectionPart.setSelection(compositionSelection);
		compositeDetailsSectionPart.setSelection(compositionSelection);

		compositeTableSectionPart
				.addSelectionListener(new ISelectionListener<CompositionSelection>() {

					@Override
					public void selectionChanged(CompositionSelection selection) {
						log.debug("Setting selection to Composite Details Section");
						compositeDetailsSectionPart.setSelection(selection);
					}
				});

		compositeDetailsSectionPart
				.addSelectionListener(new ISelectionListener<CompositionSelection>() {

					@Override
					public void selectionChanged(CompositionSelection selection) {
						log.debug("Setting selection to Composite Table Section");
						compositeTableSectionPart.setSelection(selection);
						log.debug("Setting selection to Composite Details Section");
						compositeDetailsSectionPart.setSelection(selection);
					}
				});

	}

	@Override
	public void setFocus() {
	}

}
